package com.kly.cashmall.module.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.kly.cashmall.R;
import com.kly.cashmall.base.BaseBarActivity;
import com.kly.cashmall.bean.RepayDetailsBean;
import com.kly.cashmall.framework.mvp.PresenterLifeCycle;
import com.kly.cashmall.module.order.adapter.GeneralInformationDisplayAdapter;
import com.kly.cashmall.module.order.presenter.LoanRepayDetailsPresenter;
import com.kly.cashmall.module.order.presenter.LoanRepayDetailsViewer;
import com.kly.cashmall.utils.data.CollectionUtils;


public class LoanRepayDetailsActivity extends BaseBarActivity implements LoanRepayDetailsViewer {

    private static final String BILLNO = "billNo";
    @PresenterLifeCycle
    LoanRepayDetailsPresenter presenter = new LoanRepayDetailsPresenter(this);
    private ListView mRepayDetailList;
    private GeneralInformationDisplayAdapter mAdapter;

    public static Intent newIntent(Context context, String billNo) {
        Intent intent = new Intent(context, LoanRepayDetailsActivity.class);
        intent.putExtra(BILLNO, billNo);
        return intent;
    }

    @Override
    protected void setView(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_loan_repay_details);
        setTitle("Repay Details");

        mRepayDetailList = bindView(R.id.repay_detail_list);
        mAdapter = new GeneralInformationDisplayAdapter(this);
        mRepayDetailList.setAdapter(mAdapter);
    }

    @Override
    protected void loadData() {
        presenter.getOrderRepayDetail(getIntent().getStringExtra(BILLNO));
    }

    @Override
    public void getOrderRepayDetailSuccess(RepayDetailsBean repayDetailsBean) {
        if (repayDetailsBean != null) {
            bindView(R.id.repay_detail_content,true);
            SpannableString string = new SpannableString(repayDetailsBean.getAmount());
            if (repayDetailsBean.getAmount().contains("₹")) {
                string.setSpan(new RelativeSizeSpan(0.78f), repayDetailsBean.getAmount().indexOf("₹"), repayDetailsBean.getAmount().indexOf("₹") + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            bindText(R.id.repay_statue_text, repayDetailsBean.getStatusText());
            bindText(R.id.repay_deta, repayDetailsBean.getRepayDate());
            bindText(R.id.repay_plan_amount, string);
            bindText(R.id.repay_plan, "Plan " + repayDetailsBean.getCurrentPeriod());
            bindText(R.id.repay_pricipal, "Principal: " + repayDetailsBean.getPrinciple());
            bindText(R.id.repay_interest, "Interest: " + repayDetailsBean.getInterest());
            if (!TextUtils.isEmpty(repayDetailsBean.getOverdueFees())) {
                bindText(R.id.repay_over_fee, "Over fee: " + repayDetailsBean.getOverdueFees());
            }

            if (!CollectionUtils.isEmpty(repayDetailsBean.getOrderFieldDetails())) {
                mAdapter.setOrderFieldDetailsBean(repayDetailsBean.getOrderFieldDetails());
            }
        }

    }
}
